#include <QVBoxLayout>
#include <QToolBar>
#include <QComboBox>
#include <QAction>
#include <QScrollArea>
#include <QLabel>

#include "containerwidget.h"
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent)
//    ui(new Ui::Widget)
{
//    ui->setupUi(this);
    init();
    setWindowTitle(tr("Haha Crawler"));
}

Widget::~Widget()
{
//    delete ui;
}

QSize Widget::sizeHint() const
{
    return QSize(400, 400);
}

void Widget::init()
{
    m_container = new ContainerWidget(this);

    QVBoxLayout* layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    QToolBar* toolbar = new QToolBar(tr("Command"), this);
    toolbar->addWidget(new QLabel(tr("Category:"), this));

    m_imageCategoryCombo = new QComboBox(this);
    m_imageCategoryCombo->addItems(QStringList() << tr("Good") << tr("New"));
    toolbar->addWidget(m_imageCategoryCombo);
    toolbar->addSeparator();
    connect(m_imageCategoryCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCategoryComboIndexChanged(int)));

    toolbar->addWidget(new QLabel(tr("Image Count Per Page")));
    m_imageCountCombo = new QComboBox(this);
    m_imageCountCombo->addItems(QStringList() << "5" << "10" << "15" << "20");
    connect(m_imageCountCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onImageCountComboIndexChanged(int)));
    toolbar->addWidget(m_imageCountCombo);
    toolbar->addSeparator();

    QAction* nextAct = new QAction(this);
    nextAct->setText(tr("Next"));
    connect(nextAct, SIGNAL(triggered()), m_container, SLOT(next()));
    toolbar->addAction(nextAct);

    QAction* clearAct = new QAction(this);
    clearAct->setText(tr("Clear"));
    connect(clearAct, SIGNAL(triggered()), m_container, SLOT(clear()));
    toolbar->addAction(clearAct);

    layout->addWidget(toolbar);
    layout->addWidget(m_container);
    setLayout(layout);
}

void Widget::onCategoryComboIndexChanged(int index)
{
    m_container->setImageCategory(static_cast<ContainerWidget::ImageCategory>(index));
}

void Widget::onImageCountComboIndexChanged(int index)
{
    Q_UNUSED(index)
    m_container->setImageCountPerPage(m_imageCountCombo->currentText().toInt());
}

